package com.fr.chart.core.glyph;

import com.fr.base.background.Background;
import com.fr.base.background.IntervalColorBackground;
import com.fr.base.core.IteratorChain;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.GeneralInfo;
import com.fr.chart.LineStyleInfo;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.FoldLine;
import com.fr.chart.plot.DataPoint;
import com.fr.chart.plot.DataSeries;
import com.fr.chart.plot.MarkerGlyph;
import com.fr.util.Utils;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class XYScatterPlotGlyph extends PlotGlyph
{
  private static final long serialVersionUID = 64495889773358205L;
  public static final String XML_TAG = "XYScatterPlotGlyph";
  private ValueAxisGlyph xAxisGlyph;
  private ValueAxisGlyph yAxisGlyph;
  private boolean showLine = true;
  private boolean showMarker = true;

  public void layoutAxisGlyph()
  {
    Rectangle2D localRectangle2D = getBounds();
    this.xAxisGlyph.dealPlotBoundsWithAxisLabel(localRectangle2D);
    this.yAxisGlyph.dealPlotBoundsWithAxisLabel(localRectangle2D);
    this.xAxisGlyph.calculateAxisGlyph(localRectangle2D);
    this.yAxisGlyph.calculateAxisGlyph(localRectangle2D);
    setBounds(localRectangle2D);
    Background localBackground = getInfo().getBackground();
    if (localBackground instanceof IntervalColorBackground)
    {
      IntervalColorBackground localIntervalColorBackground = (IntervalColorBackground)localBackground;
      localIntervalColorBackground.setIntervalLength(this.yAxisGlyph.getUnit() * this.yAxisGlyph.getUnitLen());
    }
  }

  public void layoutDataSeriesGlyph()
  {
    int i = 0;
    int j = getSeriesSize();
    while (i < j)
    {
      DataSeries localDataSeries = getSeries(i);
      int k = 0;
      for (int l = 0; l < localDataSeries.getDataPointCount(); ++l)
        if (!(localDataSeries.getDataPoint(l).isValueIsNull()))
          ++k;
      double[] arrayOfDouble1 = new double[k];
      double[] arrayOfDouble2 = new double[k];
      k = 0;
      GeneralPath localGeneralPath = new GeneralPath();
      int i1 = 1;
      for (int i2 = 0; i2 < localDataSeries.getDataPointCount(); ++i2)
      {
        DataPoint localDataPoint = localDataSeries.getDataPoint(i2);
        if (localDataPoint.isValueIsNull())
        {
          if (isNullValueBreak())
            i1 = 1;
        }
        else
        {
          double d1;
          Number localNumber = Utils.string2Number(localDataPoint.getCategoryName());
          if (localNumber == null)
            d1 = this.xAxisGlyph.getPoint2D(0.0D).getX();
          else
            d1 = this.xAxisGlyph.getPoint2D(localNumber.doubleValue()).getX();
          double d2 = localDataPoint.getValue();
          double d3 = this.yAxisGlyph.getBounds().getY() + this.yAxisGlyph.getPoint2D(d2).getY();
          if (i1 != 0)
          {
            localGeneralPath.moveTo((float)d1, (float)d3);
            i1 = 0;
          }
          else
          {
            localGeneralPath.lineTo((float)d1, (float)d3);
          }
          arrayOfDouble1[k] = d1;
          arrayOfDouble2[k] = d3;
          ++k;
          if (this.showMarker)
          {
            MarkerGlyph localMarkerGlyph = new MarkerGlyph();
            localMarkerGlyph.dealCondition4Line(getSeriesCollection(), i);
            localMarkerGlyph.setShape(new Rectangle2D.Double(d1 - 5.0D, d3 - 5.0D, 10.0D, 10.0D));
            localDataPoint.setDrawImpl(localMarkerGlyph);
          }
          else
          {
            localDataPoint.setShape(new Rectangle2D.Double(d1 - 5.0D, d3 - 5.0D, 10.0D, 10.0D));
          }
          dealDataPointLabelBounds(localDataPoint, i);
        }
      }
      if (this.showLine)
      {
        FoldLine localFoldLine = new FoldLine();
        localDataSeries.setDrawImpl(localFoldLine);
        localFoldLine.setGeneralPath(localGeneralPath);
        localFoldLine.getLineStyleInfo().dealCondition(getSeriesCollection(), i);
      }
      trendLineFitting(arrayOfDouble1, arrayOfDouble2, i);
      ++i;
    }
  }

  public Iterator selectableChildren()
  {
    ArrayList localArrayList = new ArrayList();
    if (this.xAxisGlyph != null)
      localArrayList.add(this.xAxisGlyph);
    if (this.yAxisGlyph != null)
      localArrayList.add(this.yAxisGlyph);
    return new IteratorChain(new Iterator[] { super.selectableChildren(), localArrayList.iterator() });
  }

  public void draw(Graphics paramGraphics)
  {
    drawInfo(paramGraphics);
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    localGraphics2D.translate(getBounds().getX(), getBounds().getY());
    if (this.xAxisGlyph != null)
      this.xAxisGlyph.draw(localGraphics2D);
    if (this.yAxisGlyph != null)
      this.yAxisGlyph.draw(localGraphics2D);
    localGraphics2D.translate(-getBounds().getX(), -getBounds().getY());
    super.draw(localGraphics2D);
  }

  public void setXAxisGlyph(ValueAxisGlyph paramValueAxisGlyph)
  {
    this.xAxisGlyph = paramValueAxisGlyph;
  }

  public ValueAxisGlyph getXAxisGlyph()
  {
    return this.xAxisGlyph;
  }

  public void setYAxisGlyph(ValueAxisGlyph paramValueAxisGlyph)
  {
    this.yAxisGlyph = paramValueAxisGlyph;
  }

  public ValueAxisGlyph getYAxisGlyph()
  {
    return this.yAxisGlyph;
  }

  public void setShowLine(boolean paramBoolean)
  {
    this.showLine = paramBoolean;
  }

  public boolean isShowLine()
  {
    return this.showLine;
  }

  public void setShowMarker(boolean paramBoolean)
  {
    this.showMarker = paramBoolean;
  }

  public boolean isShowMarker()
  {
    return this.showMarker;
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("XYScatterPlotGlyph");
    super.writeXML(paramXMLPrintWriter);
    if (this.xAxisGlyph != null)
    {
      paramXMLPrintWriter.startTAG("XAxisGlyph");
      this.xAxisGlyph.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    if (this.yAxisGlyph != null)
    {
      paramXMLPrintWriter.startTAG("YAxisGlyph");
      this.yAxisGlyph.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    paramXMLPrintWriter.end();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if (str.equals("XAxisGlyph"))
        this.xAxisGlyph = ((ValueAxisGlyph)paramXMLableReader.readXMLObject(new ValueAxisGlyph()));
      else if (str.equals("YAxisGlyph"))
        this.yAxisGlyph = ((ValueAxisGlyph)paramXMLableReader.readXMLObject(new ValueAxisGlyph()));
    }
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof XYScatterPlotGlyph))
      return false;
    XYScatterPlotGlyph localXYScatterPlotGlyph = (XYScatterPlotGlyph)paramObject;
    if (!(super.equals(localXYScatterPlotGlyph)))
      return false;
    if (!(Equals.equals(localXYScatterPlotGlyph.xAxisGlyph, this.xAxisGlyph)))
      return false;
    return (Equals.equals(localXYScatterPlotGlyph.yAxisGlyph, this.yAxisGlyph));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    XYScatterPlotGlyph localXYScatterPlotGlyph = (XYScatterPlotGlyph)super.clone();
    if (this.xAxisGlyph != null)
      localXYScatterPlotGlyph.xAxisGlyph = ((ValueAxisGlyph)this.xAxisGlyph.clone());
    if (this.yAxisGlyph != null)
      localXYScatterPlotGlyph.yAxisGlyph = ((ValueAxisGlyph)this.yAxisGlyph.clone());
    return localXYScatterPlotGlyph;
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    if (this.xAxisGlyph != null)
      localJSONObject.put("xAxisGlyph", this.xAxisGlyph.toJSONObject());
    if (this.yAxisGlyph != null)
      localJSONObject.put("yAxisGlyph", this.yAxisGlyph.toJSONObject());
    localJSONObject.put("showLine", this.showLine);
    localJSONObject.put("showMarker", this.showMarker);
    return localJSONObject;
  }

  public String getPlotGlyphType()
  {
    return "XYScatterPlotGlyph";
  }
}